package com.aqiang.day0421.view.fagment;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.aqiang.core.mvp.view.BaseFragment;
import com.aqiang.day0421.R;
import com.aqiang.day0421.adapter.TwoAdapter;
import com.aqiang.day0421.contarct.TwoContract;
import com.aqiang.day0421.entity.XiaEntity;
import com.aqiang.day0421.model.TwoModel;
import com.aqiang.day0421.presenter.TwoPresenter;
import com.aqiang.day0421.sp.SPutlis;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TwoFragment extends BaseFragment<TwoPresenter> implements TwoContract.TwoView {
    @BindView(R.id.banner_fragment_two)
    Banner bannerFragmentTwo;
    @BindView(R.id.rv_fragment_two)
    RecyclerView rvFragmentTwo;
    Unbinder unbinder;

    private TwoAdapter xiaAdapter;
    @Override
    protected int bindLayout() {
        return R.layout.fragment_two;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, this.getView());
        rvFragmentTwo.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    protected void initData() {
        mPresenter = new TwoPresenter(new TwoModel(),this);
        mPresenter.xia();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void initBanner(List<String> list) {
        bannerFragmentTwo.setImages(list);
        bannerFragmentTwo.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        bannerFragmentTwo.start();
    }

    @Override
    public void initAdapter(List<XiaEntity.DataBean> list) {

        //bananer
        ArrayList<String> strings = new ArrayList<>();
        for (XiaEntity.DataBean dataBean : list) {
            strings.add(dataBean.getPic());
        }
        initBanner(strings);
        if(xiaAdapter == null){
            xiaAdapter = new TwoAdapter(R.layout.item_xia,list);
            rvFragmentTwo.setAdapter(xiaAdapter);
        }else {
            xiaAdapter.getData().addAll(list);
            xiaAdapter.notifyDataSetChanged();
        }
        SharedPreferences sharedPreferences = SPutlis.getInstance().getSP(getContext(), "day21");
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("isList",true);
        edit.commit();
    }
}
